<template>
  <div class="boomGoodsDetails">
    <div class="points_main">
      <div class="goods">
        <div class="Img">
          <img :src="goodsList.pictrue" />
        </div>
        <span class="name">{{ goodsList.goodsName }}</span>
        <div class="center">
          <div class="jifen">
            所需爆品金:
            <img src="../../assets/image/moliy.png" alt />
            <i>{{ goodsList.costBalance }}</i>
          </div>
          <div class="moliy" v-show="showSku">
            <p>规格:</p>
            <div
              v-for="(item, idx) in goodsList.skuSpecList"
              :key="idx"
              :class="{
                disabled: item.stock == 0,
                normal: item.normal != idx && item.stock != 0,
                checked: item.normal == idx && item.stock != 0,
                common:true
              }"
              @click="selectSku(item, idx)"
            >
              <span class="bgColor"></span>
              <span>{{ item.skuName }}</span>
            </div>
          </div>
          <div class="ku">库存:{{ goodsList.stock }}</div>
        </div>
      </div>
      <div class="goo">
        <p>采购数量</p>
        <div class="num">
            <span @click="sub()">-</span>
            <input type="text" v-model="num" @input="showNum()" />
            <span @click="add()">+</span>
        </div>
      </div>
      <div class="shop_xq">
        <span class="lipin">礼品详情</span>
        <div v-html="goodsList.detail" class="detail"></div>
        <div class="Img_item" v-for="(v,i) in goodsList.imgs" :key='i'>
          <img :src="v" alt />
        </div>
      </div>
    </div>
    <div class="footer">
      <div class="dui" @click="toWriteOrder()">立即兑换</div>
    </div>
  </div>
</template>

<script>
//引入保存所有接口的url文件
import url from "../../axios/apiUrl";
//引入api.js定义的方法index
import { postRequest } from "../../api/index";
import { getRequest } from "../../api/indexGet";
export default {
  data() {
    return {
      ulIndex: 0, //向上移动几个li
      marginTop: 0, //向上向下移动的距离
      isShow: false, //控制遮罩层marks和大图片是否显示"
      left: 0, //marks左移位置
      top: 0, //marks下移位置
      leftLgImg: 0, //大图lgImg移动的位置
      topLgImg: 0, //大图lgImg移动的位置
      bigImg: "",
      showImg: false, //是否显示小图片的上下按钮
      goodsList: {}, // 商品详情列表
      num: 1,
      sku: "",
      showSku: false
    };
  },
  created() {
    getRequest(url.boomGoodsDetails + "/" + this.$route.query.id).then(res => {
      this.goodsList = res.data.content;
      if (this.goodsList.skuSpecList.length != 0) {
        this.showSku = true;
        this.sku = this.goodsList.skuSpecList[0];
        this.goodsList.stock=this.sku.skuStock
        this.goodsList.skuSpecList.map(obj => {
          this.$set(obj, "normal", 0);
        });
      }
    });
  },
  methods: {
    // 加
    add() {
      if (this.num < this.goodsList.stock) {
        this.num = parseInt(this.num) + 1;
      } else {
        this.num = this.goodsList.stock;
        this.stock--;
        this.$toast("数量不能再加了！");
      }
    },
    // 减
    sub() {
      if (this.num > 1) {
        this.num = parseInt(this.num) - 1;
      } else {
        this.num = 1;
      }
    },
    // 失焦
    showNum() {
      if (parseInt(this.num) >= parseInt(this.goodsList.stock)) {
        this.num = parseInt(this.goodsList.stock);
      } else if (this.num < 1) {
        this.num = parseInt(1);
      }
    },

    // 提交订单
    toWriteOrder() {
      if (this.num <= this.goodsList.stock&&this.num>0) {
        getRequest(url.boomGoldMoney).then(res => {
          if (res.data.content.InVogueCoin > 0) {
            if (
              res.data.content.InVogueCoin >=
              this.num * this.goodsList.costBalance
            ) {
              var result = {
                goodsId: this.goodsList.goodsId,
                quantity: this.num,
                skuName: this.sku.skuName,
                skuId: this.sku.skuId
              };
              sessionStorage.setItem("ponitOrder", JSON.stringify(result));
              this.$router.push("/boomWriteOrder");
            } else {
              this.$toast("您的爆品金不足");
            }
          } else {
            this.$toast("您的爆品金不足");
          }
        });
      }else{
        this.$toast('请输入正确的商品数量')
      }
    },
    selectSku(item, idx) {
      this.goodsList.skuSpecList.map(obj => {
        obj.normal = idx;
      });
      this.sku = item;
      this.goodsList.stock=item.skuStock
    }
  }
};
</script>
<style lang="less" scoped>
.boomGoodsDetails {
  margin: 0;
  .points_main {
    .goods {
      background: #ffffff;
      padding: 0.26rem;
      border-bottom: 0.26rem solid #f1f2f6;
      .Img {
        text-align: center;
        margin: 0.2rem auto;
        width: 460px;
        height: 460px;
        img {
          width: 460px;
          height: 460px;
        }
      }
      .name {
        width: 100%;
        text-align: center;
        font-size: 0.36rem;
        font-family: PingFang SC;
        font-weight: bold;
        color: #343434;
        line-height: 0.48rem;
        margin: 0 auto;
        margin-top: 0.36rem;
        margin-bottom: 0.36rem;
      }
      .center {
        margin-top: 0.26rem;
        line-height: 0.58rem;

        .jifen {
          color: #343434;
          font-size: 0.36rem;
          font-weight: bold;
          img {
            width: 0.29rem;
            height: 0.29rem;
          }
          i {
            color: #1fb808;
          }
        }
        .moliy {
          margin-top: 10px;
          display: flex;
          align-items: center;
          flex-wrap: wrap;
          .common {
            margin-bottom: 20px;
            min-width:88px;
            height: 70px;
            line-height: 70px;
            padding: 0 20px;
          }
          .skuNames {
            width: 1.28rem;
            height: 0.6rem;
            span {
              width: 1.28rem;
              height: 0.6rem;
              border-radius: 0.35rem;
              text-align: center;
              align-items: center;
              margin-left: 0.2rem;
            }
          }
          .checked {
            border-radius:20px;
            text-align: center;
            align-items: center;
            margin-left: 0.2rem;
            border: 1px solid #3ebc2c;
            color: #3ebc2c;
            position: relative;
            .bgImg {
              position: absolute;
              width: 24px;
              height: 22px;
              background: url("../../assets/img/jfDetails1.png") no-repeat 0 0;
              right: -1px;
              bottom: 0px;
            }
          }
          .normal {
            color: #343434;
            border: 1px solid #dadada;
            border-radius: 0.35rem;
            text-align: center;
            align-items: center;
            margin-left: 0.2rem;
          }
          .disabled {
            border: 1px solid #efefef;
            color: #9a9a9a;
          }
        }
        .noliy {
          color: #666666;
          font-size: 0.26rem;
          font-weight: 400;
        }
        .ku {
          color: #666666;
          font-size: 0.26rem;
          font-weight: 400;
        }
      }
    }
    .goo {
      height: 1rem;
      padding: 0.26rem;
      border-bottom: 0.26rem solid #f1f2f6;
      color: #343434;
      display: flex;
      align-items: center;
      p {
        font-size: 26px;
        color: #333;
      }
      span {
        font-size: 28px;
        padding-left: 0.26rem;
        padding-right: 0.26rem;
      }
      input {
        width: 1.8rem;
        height: 0.7rem;
        border: none;
        border: 1px solid #eaeaea;
        border-radius: 0.35rem;
        text-align: center;
      }
    }
    .shop_xq {
      .lipin {
        height: 1rem;
        line-height: 1rem;
        color: #1eb808;
        border-left: 0.1rem solid #1eb808;
        padding-left: 0.2rem;
      }
      .detail{
        img{
          vertical-align: middle;
        }
      }
      .Img_item {
        text-align: center;
        margin: 0 auto 80px;
        display: grid;
        img {
          width: 100%;
          margin: 0 auto;
        }
      }
    }
  }
  .footer {
    z-index: 999;
    height: 80px;
    display: flex;
    justify-content: flex-end;
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
    border: 0.01rem solid #f1f1f1;
    background: #ffffff;
    text-align: -webkit-center;
    .foot {
      width: 2.8rem;
      height: 80px;
      margin: 0 auto;
      // display: flex;
      // flex-direction: column;
      img {
        width: 0.48rem;
        height: 0.48rem;
        margin-bottom: 0.12rem;
      }
    }
    .jia {
      width: 2.8rem;
      height: 1rem;
      background: #1eb808;
      line-height: 1rem;
      align-items: center;
      text-align: center;
      color: #ffffff;
    }
    .dui {
      width: 2.8rem;
      height: 1rem;
      float: left;
      background: #fe323c;
      align-items: center;
      text-align: center;
      color: #ffffff;
      line-height: 1rem;
    }
  }
}
</style>